import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Confirm from '#cpt/temp/Confirm'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const Modal 				= $Lazy.state(()=>import('./cpt/modal'))
// ===================================================================== data
const statusData = [{ name:'待审核', value:'1' },{ name:'审核通过', value:'2' },{ name:'审核未通过', value:'3' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '状态',		name: 'status',	type: 'select', data:statusData },
			{ label: '模板名称',		name: 'qt_temp_id', type: 'select', loading:true, idStr:'uuid' },
			{ label: '审核时间', 	names: ['start_at','end_at'], type:'date-range' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 模板名称
			await $fn.setRowc(forms, 'qt_temp_id',  async () =>  f.getCache({ api: 'qt_template'}))
			this.setState({ forms })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'qt-item-company-audit/index')
	
	// 表单
		cols = [
			{ title: '模板名称',		dataIndex: 'temp_name', 	width:120 },
			{ title: '物价代码', 	dataIndex: 'price_code', 		width:100 },
			{ title: '项目名称', 	dataIndex: 'item_name', 		width:150 },
			{ title: '申请人', 		dataIndex: 'created_user', 		...$fn.table.name },
			{ title: '审核人', 		dataIndex: 'audit_user', 		...$fn.table.name },
			{ title: '普通价格', 	dataIndex: 'hosp_price', 		...$fn.table.num },
			{ title: '三甲价格', 	dataIndex: 'hosp_price_ta', 	...$fn.table.num },
			{ title: '三乙价格', 	dataIndex: 'hosp_price_tb', 	...$fn.table.num },
			{ title: '二甲价格', 	dataIndex: 'hosp_price_sa', 	...$fn.table.num },
			{ title: '二乙价格', 	dataIndex: 'hosp_price_sb', 	...$fn.table.num },
			{ title: '状态', 		dataIndex: 'status_name', 		...$fn.table.status },
			{ title: '申请时间', 	dataIndex: 'created_at', 		...$fn.table.time },
			{ title: '审核时间', 	dataIndex: 'audit_at', 			...$fn.table.time },
			{ title: '备注', 		dataIndex: 'remark', 			width:150 },
			TableControls.controls.call(this)	
		]
	// 表格操作按钮
	table_controls = (text,{ status,uuid }) => {
		return [
			+status === 1 && TableControls.click.call(this, '拒绝', ()=>this.modalRef.open([uuid]), {isKeys:true} ),
			+status === 1 && Confirm.call(this, '通过', 'qt-item-company-audit/audit',{ msg:`是否确认审核通过？`, query:{uuid:[uuid],status:2} } ),
		]
	}
	// 新增通过，拒绝按钮 耿琰 20220422
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows } = this.state
		let uuids = []
		selectedRows.map(v=>{
			if(+v.status === 0) uuids.push(v.uuid)
			return uuids
		})
		return [
			Controls.click.call(this, '拒绝', ()=>this.modalRef.open(selectedKeys), {isKeys:true} ),
			Confirm.call(this, '通过', 'qt-item-company-audit/audit',{ msg:`共计【${selectedKeys.length}】条数据，是否确认审核通过？`, isKeys:true, query:(keys,rows)=> {
				let uuid = []
				rows.forEach((v, i)=> {
					uuid[i] = v.uuid
				})
				return {uuid,status:2}
			}} ),
		]
	}
	
	render(){
		return (
			<>
				<Page title='模板物价审核' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
					{/* 操作详情 */}
					<Modal onRef={ref=>this.modalRef=ref} onRefresh={this.fetch} />
				</Page>
			</>
		)
	}
}

export default Index